# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from scrapy.pipelines.images import ImagesPipeline, FilesPipeline
import scrapy
from . import utils, settings

# 下载 Acgn 图片管道
class AcgnscrapyPipeline(ImagesPipeline):
    debug_mode = False
    # 取得图片请求
    def get_media_requests(self, item, info):
        site = item.get('site', '')
        if site == "acgn":
            topic = item.get('title', '')
            id = item.get('id', '')
            detail_url = item.get('detail_url', '')
            image_list = item.get('image_list', [])
            total = utils.check_directory(topic, "", id, settings.ROOT_PATH)
            for image_url in image_list:
                if image_url != '':
                    filename = str(image_url.split('/')[-1])
                    extname = image_url.split('.')[-1]
                    filename = filename.replace(f'.{extname}', "").zfill(6)
                    info = utils.get_info("", id, image_url, filename)
                    result = utils.check_file(topic, info, settings.ROOT_PATH)
                    if self.debug_mode:
                        print(f'AcgnscrapyPipeline.get_media_requests id={id}, image_url={image_url}, filename={filename}.{extname}')
                    if result["new_url"] != "":
                        yield scrapy.Request(result["new_url"], meta=result)

    # 指定图片的档案名称
    def file_path(self, request, response=None, info=None):
        if self.debug_mode:
            print(f'AcgnscrapyPipeline.file_path folder={request.meta["folder"]}, file_name={request.meta["file_name"]}, new_url={request.meta["new_url"]}')
        file_name = request.meta['file_name'] or ''
        return file_name

    # 结束下载
    def item_completed(self, results, item, info):
        return item
    

# 下载 DM5 图片管道
class Dm5scrapyPipeline(ImagesPipeline):
    debug_mode = False
    # 取得图片请求
    def get_media_requests(self, item, info):
        site = item.get('site', '')
        if site == "dm5":
            topic = item.get('topic', '')
            id = item.get('id', '')
            id = str(id).zfill(6)
            #detail_url = item.get('detail_url', '')
            image_list = item.get('image_list', [])
            total = utils.check_directory(topic, "", id, settings.ROOT_PATH)
            idx = 1
            for image_url in image_list:
                if image_url != '':
                    extname = image_url.split('?')[0]
                    extname = extname.split('.')[-1]
                    filename = str(idx).zfill(6)
                    idx = idx + 1
                    info = utils.get_info("", id, image_url, filename)
                    result = utils.check_file(topic, info, settings.ROOT_PATH)
                    if self.debug_mode:
                        print(f'Dm5scrapyPipeline.get_media_requests id={id}, image_url={image_url}, filename={filename}.{extname}')
                    if result["new_url"] != "":
                        yield scrapy.Request(result["new_url"], meta=result)

    # 指定图片的档案名称
    def file_path(self, request, response=None, info=None):
        if self.debug_mode:
            print(f'Dm5scrapyPipeline.file_path folder={request.meta["folder"]}, file_name={request.meta["file_name"]}, new_url={request.meta["new_url"]}')
        file_name = request.meta['file_name'] or ''
        return file_name

    # 结束下载
    def item_completed(self, results, item, info):
        return item
    

# 下载 Taiwanmh 图片管道
class TaiwanmhPipeline(ImagesPipeline):
    debug_mode = True
    # 取得图片请求
    def get_media_requests(self, item, info):
        site = item.get('site', '')
        if site == "taiwanmh":
            topic = item.get('title', '')
            id = item.get('id', '')
            image_url = item.get('image_url', '')
            image_index = item.get('image_index', 0)
            print(f'TaiwanmhPipeline.get_media_requests id={id}, image_url={image_url}, filename={filename}.{extname}')
            if image_url != '':
                extname = image_url.split('.')[-1]
                extname = extname.split('?')[0]
                filename = f'{str(image_index).zfill(6)}' #.{extname}'
                info = utils.get_info("", id, image_url, filename)
                result = utils.check_file(topic, info, settings.ROOT_PATH)
                if self.debug_mode:
                    print(f'TaiwanmhPipeline.get_media_requests id={id}, image_url={image_url}, filename={filename}.{extname}')
                if result["new_url"] != "":
                    yield scrapy.Request(result["new_url"], meta=result)

    # 指定图片的档案名称
    def file_path(self, request, response=None, info=None):
        if self.debug_mode:
            print(f'TaiwanmhPipeline.file_path folder={request.meta["folder"]}, file_name={request.meta["file_name"]}, new_url={request.meta["new_url"]}')
        file_name = request.meta['file_name'] or ''
        return file_name

    # 结束下载
    def item_completed(self, results, item, info):
        return item
    